package effect
{
	
	import flash.geom.Point;
	
	import control.Control;
	
	import game.bejeweled.BackDrop;
	import game.bejeweled.Ball;
	
	import models.BallPool;
	import models.TweenCounter;
	
	import starling.animation.Transitions;
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.Sprite;
	import starling.events.Event;
	
	public class Effect extends Sprite
	{
		private var ballPool:BallPool
		public function Effect()
		{
			super();
			ballPool=new BallPool();
		}
		
		public function moveBallEffect(ball:Ball,endP:Point,tname:String="moveball"):void{
			var tween:Tween=new Tween(ball,0.3,Transitions.EASE_OUT);
			tween.moveTo(endP.x,endP.y);
			Starling.juggler.add(tween);
			TweenCounter.instant.addCounter(tname);
			tween.onComplete=function subcounter():void{
				TweenCounter.instant.subCounter(tname,function fun():void{
					dispatch(tname);
				});
			}
		}
		
		public function removeBallEvent(backdrop:BackDrop,tname:String="removeball"):void{
			var currBall:Ball=backdrop.currBall;
			var tween:Tween=new Tween(currBall,0.5,Transitions.EASE_IN_ELASTIC);
			tween.animate("alpha",0.5);
			tween.scaleTo(0.2);
			Starling.juggler.add(tween);
			TweenCounter.instant.addCounter(tname);
			tween.onComplete=function subcounter():void{
				BallPool.instant.returnClass(currBall);
				backdrop.isFull=false;
				TweenCounter.instant.subCounter(tname,function fun():void{
					dispatch(tname);
				});
			}
		}
		
		public function shootBallEff(backdrop:BackDrop,shootP:Point,tname:String="removeball"):void{
			var currBall:Ball=backdrop.currBall;
			currBall.touchable=false;
			var tween:Tween=new Tween(currBall,0.5,Transitions.EASE_IN_BACK);
//			tween.moveTo(shootP.x,shootP.y);
			
			tween.scaleTo(0.5);
			
			Starling.juggler.add(tween);
			TweenCounter.instant.addCounter(tname);
			tween.onComplete=function subcounter():void{
				backdrop.isFull=false;
				TweenCounter.instant.subCounter(tname,function fun():void{
					dispatch(tname);
				});
				
				currBall.TargetP=new Point(700,200);
				Control.instant.setRunVec(currBall);
//				BallPool.instant.returnClass(currBall);
			}
		}
		
		
		private function dispatch(evtName:String):void{
			this.dispatchEvent(new Event(evtName+"_complete"));
		}
		
		private var sTween:Tween;
		public function lightning(obj:DisplayObject):Tween{
			Starling.juggler.removeTweens(sTween);
			sTween=new Tween(obj,1);
			sTween.animate("alpha",0.5);
			sTween.scaleTo(1.2);
			sTween.repeatCount=Math.max();
			Starling.juggler.add(sTween);
			return sTween;
		}
		
		//*****************************************************
		// Singleton 
		//*****************************************************
		private static var _instant:Effect;
		public static function get instant():Effect
		{
			if(null == _instant)
			{
				_instant =  new Effect();
			}
			return _instant;
		}
	}
}